﻿using System;
using System.Net;
using Imgur.Net.Auth;
using Imgur.Net;
using Imgur.Net.Util;
using NUnit.Framework;
using TestableHttpWebResponse;

namespace Imgur.Net.Test.ImageProvider
{
    [TestFixture]
    public class UpdateImageTests
    {

        private const string imageDeletehash = "271dKcJbcPw5MDp";

        private const string clientId = "b56c4ebcd84a2c5";
        private const string clientSecret = "5b83ef6e0100a5a8544cc03f2e49c470d7f7cb2f";

        private const string baseTestUriTemplate = "test://api.imgur.com/3/image";
        //private const string baseTestUriTemplate = "https://api.imgur.com/3/image";

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            WebRequest.RegisterPrefix("test", TestableWebRequestCreateFactory.GetFactory());
        }

        [Test]
        public void TestSuccessUpdateImage()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            var imgProvider = new DefaultImageProvider(authProvider, baseTestUriTemplate);

            string title = "A New Title";
            string description = "A New Description";


            Uri uri = new Uri(string.Format(baseTestUriTemplate + "/{0}", imageDeletehash))
                        .AddQuery("title", title)
                        .AddQuery("description", description);

            var expectedRequest = new TestableWebRequest(uri);

            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success",
                ResourceLoader.GetResourceText("Resources/Result.true.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            bool? result = imgProvider.UpdateImageInfo(imageDeletehash, title, description);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Value);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestUpdateImageNullImageId()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            var imgProvider = new DefaultImageProvider(authProvider, baseTestUriTemplate);

            string title = "A New Title";
            string description = "A New Description";

            Uri uri = new Uri(string.Format(baseTestUriTemplate + "/{0}", imageDeletehash))
                        .AddQuery("title", title)
                        .AddQuery("description", description);

            var expectedRequest = new TestableWebRequest(uri);

            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success",
                ResourceLoader.GetResourceText("Resources/Result.true.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            bool? result = imgProvider.UpdateImageInfo(null, title, description);
        }

        [Test]
        public void TestFailUpdateImage()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            var imgProvider = new DefaultImageProvider(authProvider, baseTestUriTemplate);

            string title = "A New Title";
            string description = "A New Description";


            Uri uri = new Uri(string.Format(baseTestUriTemplate + "/{0}", imageDeletehash))
                        .AddQuery("title", title)
                        .AddQuery("description", description);

            var expectedRequest = new TestableWebRequest(uri);
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success",
                ResourceLoader.GetResourceText("Resources/Result.false.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            
            bool? result = imgProvider.UpdateImageInfo(imageDeletehash, title, description);

            Assert.IsNotNull(result);
            Assert.IsFalse(result.Value);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestNullTitleAndDescriptionUpdateImage()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            var imgProvider = new DefaultImageProvider(authProvider, baseTestUriTemplate);

            string title = "A New Title";
            string description = "A New Description";


            Uri uri = new Uri(string.Format(baseTestUriTemplate + "/{0}", imageDeletehash))
                        .AddQuery("title", title)
                        .AddQuery("description", description);

            var expectedRequest = new TestableWebRequest(uri);
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success",
                ResourceLoader.GetResourceText("Resources/Result.true.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            bool? result = imgProvider.UpdateImageInfo(imageDeletehash, null, null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestEmptyTitleAndDescriptionUpdateImage()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            var imgProvider = new DefaultImageProvider(authProvider, baseTestUriTemplate);

            string title = "A New Title";
            string description = "A New Description";


            Uri uri = new Uri(string.Format(baseTestUriTemplate + "/{0}", imageDeletehash))
                        .AddQuery("title", title)
                        .AddQuery("description", description);

            var expectedRequest = new TestableWebRequest(uri);
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success",
                ResourceLoader.GetResourceText("Resources/Result.true.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            bool? result = imgProvider.UpdateImageInfo(imageDeletehash, string.Empty, string.Empty);
        }
    }
}